package com.sita.bluetactivity.http;
import android.test.suitebuilder.annotation.MediumTest;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sita.bluetactivity.LoginAndRegister.ForgetRequest;
import com.sita.bluetactivity.LoginAndRegister.LoginBackBean;
import com.sita.bluetactivity.LoginAndRegister.LoginRequest;
import com.sita.bluetactivity.LoginAndRegister.MsgBackBean;
import com.sita.bluetactivity.LoginAndRegister.RegisterBackBean;
import com.sita.bluetactivity.LoginAndRegister.RegisterRequest;
import com.sita.bluetactivity.MineFragment.BindListBackBean;
import com.sita.bluetactivity.MineFragment.BindListRequest;
import com.sita.bluetactivity.MineFragment.BindVehicleBackBean;
import com.sita.bluetactivity.MineFragment.BindVehicleRequest;
import com.sita.bluetactivity.MineFragment.UnBindBackBean;
import com.sita.bluetactivity.MineFragment.UnBindVehicleRequest;
import com.sita.bluetactivity.MineFragment.UpDataBackBean;
import com.sita.bluetactivity.NewBlueTooth.BindBackBean;
import com.sita.bluetactivity.NewBlueTooth.BindVehicleMsg;
import com.sita.bluetactivity.SafeFragment.FancesBackBean;
import com.sita.bluetactivity.SafeFragment.GetFancesRequest;
import com.sita.bluetactivity.TripFragment.DayMesBackBean;
import com.sita.bluetactivity.TripFragment.DayMsgRequest;
import com.sita.bluetactivity.TripFragment.MakeTripBackBean;
import com.sita.bluetactivity.TripFragment.MakeTripRequest;
import com.sita.bluetactivity.TripFragment.TripBackBean;
import com.sita.bluetactivity.TripFragment.TripRequest;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.OkHttpClient;
import java.util.concurrent.TimeUnit;
import retrofit.Callback;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.PUT;
import retrofit.http.Part;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit.mime.TypedFile;
import retrofit.mime.TypedString;

/**
 * Created by Jason on 2016/8/1.
 */
public class RestClient {
    private static final String BASE_URL = Constants.BASE_URI;
    private static RestService restService;
    private static RestService restNormalService;
    private static Gson mGson = new GsonBuilder()
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
            .serializeNulls()
            .create();

    private static Gson mGson2 = new GsonBuilder()
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
            .create();

    private static RequestInterceptor restInterceptor = new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Content-Type", Constants.APPLICATION_JSON);
            request.addHeader("Accept", Constants.APPLICATION_JSON);
        }
    };

    private static RequestInterceptor restFormInterceptor = new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", Constants.APPLICATION_JSON);
        }
    };

    private static void createRestFormClient() {
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setWriteTimeout(120, TimeUnit.SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(BASE_URL)
                .setConverter(new GsonConverter(mGson))
                .setRequestInterceptor(restFormInterceptor)
                .setClient(new OkClient(okHttpClient))
                .build();

        restService = restAdapter.create(RestService.class);
    }

    private static void createRestNormalClient() {
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setWriteTimeout(120, TimeUnit.SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(BASE_URL)
                .setConverter(new GsonConverter(mGson))
                .setRequestInterceptor(restInterceptor)
                .setClient(new OkClient(okHttpClient))
                .build();

        restNormalService = restAdapter.create(RestService.class);
    }

    public static RestService getRestService() {
        if (restService == null) {
            createRestFormClient();
        }
        return restService;
    }
    public static RestService GetFance() {
        if (restService == null) {
            createRestFormBoDao();
        }
        return restService;
    }
    private static void createRestFormBoDao() {
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setWriteTimeout(120, TimeUnit.SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint("http://api.gpslink.cn")
                .setConverter(new GsonConverter(mGson))
                .setRequestInterceptor(restFormInterceptor)
                .setClient(new OkClient(okHttpClient))
                .build();

        restService = restAdapter.create(RestService.class);
    }
    public static RestService getRestNormalService() {
        if (restNormalService == null) {
            createRestNormalClient();
        }
        return restNormalService;
    }

    public static Gson getGson() {
        return mGson;
    }

    public static Gson getGsonNull() {
        return mGson2;
    }

    /**
     * 所有的请求直接返回实体类
     */
    public interface RestService {
        @POST("/linkboard/v1/messages/machines/route")
        void getVehicleTrip(@Body TripRequest request, Callback<TripBackBean> callback);

        //制作行程
        @POST("/linkboard/v1/messages/machines/rqtrip")
        void makeTrip(@Body MakeTripRequest request, Callback<MakeTripBackBean> callback);

//车辆列表
        @POST("/linkboard/v1/messages/machines/bindlist")
        void bindVehicleList(@Body BindListRequest request, Callback<BindListBackBean> callback);
        //登录
        @POST("/linkboard/v1/actors/app/login")
        void login(@Body LoginRequest request, Callback<LoginBackBean> callback);
        //注册
        @POST("/linkboard/v1/actors/app/register")
        void register(@Body RegisterRequest registerRequest, Callback<RegisterBackBean> callback);
        //短信验证

        //http://lin.sitayun.com:8080/linkboard/v1/actors/message/authentication/18222966432?type=1

        @GET("/linkboard/v1/actors/message/authentication/{msg}")
        void getMsg(@Path("msg")String phone, @Query("type") int type, Callback<MsgBackBean>callback);

        @PUT("/api/Fances")
        void getBoDaoLocation(@Header("Authorization")String token, @Body GetFancesRequest request, Callback<FancesBackBean> callback);
       //更新用户资料
        @Multipart
        @POST("/linkboard/v1/actors/app/update")
        void upDataUserInfo(@Part("params") TypedString typedString,@Part("avatar")TypedFile typedFile,Callback<UpDataBackBean> callback);
    //绑定车辆
        @POST("/linkboard/v1/messages/machines/bindbodao")
        void BindVehicleFromBt(@Body BindVehicleMsg bindVehicleMsg, Callback<BindBackBean>callback);
        //车辆解绑
        @POST("/linkboard/v1/messages/machine/unbindbodao")
        void UnBindVehicle(@Body UnBindVehicleRequest request, Callback<UnBindBackBean> callback);
        //忘记密码
        @POST("/linkboard/v1/actors/app/resetpassword")
        void forgetPass(@Body ForgetRequest request,Callback<LoginBackBean> callback);
        //日汇总
        @POST("/linkboard/v1/machines/machine/sum/day")
        void dayMsg(@Body DayMsgRequest request, Callback<DayMesBackBean> callback);

    }
}
